package com.example.ssrbac.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

@Service
public class WebSocketService {
    private static final Logger logger = LoggerFactory.getLogger(WebSocketService.class);

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    /**
     * 发送强制下线消息给指定用户
     * 
     * @param username 用户名
     */
    public void sendForceLogoutMessage(String username) {
        logger.info("发送强制下线消息给用户: {}", username);
        ForceLogoutMessage message = new ForceLogoutMessage(
            "FORCE_LOGOUT",
            "您的账号已被管理员强制下线，请重新登录"
        );
        messagingTemplate.convertAndSend("/topic/user/" + username, message);
    }

    public static class ForceLogoutMessage {
        private String type;
        private String message;

        public ForceLogoutMessage(String type, String message) {
            this.type = type;
            this.message = message;
        }

        public String getType() {
            return type;
        }

        public String getMessage() {
            return message;
        }
    }
} 